<?php
/**
 * Created by PhpStorm.
 * User: wangkk
 * Date: 2019-03-20 13:51
 * Description: {卡券货架的相关内容}
 */
namespace app\controllers;
use app\models\CardPageForm;
use Yii;
use app\controllers\CommonController;
use yii\data\ActiveDataProvider;
use yii\filters\VerbFilter;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;

class CardpageController extends CommonController{

    //卡券货架列表
    public function actionIndex(){
        $dataProvider = new ActiveDataProvider([
            'query' => CardPageForm::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    //添加货架
    public function actionCreate(){
        $cardpageModel = new CardPageForm();
        if ($cardpageModel->load(Yii::$app->request->post())) {
            $request = \Yii::$app->request;
            $request_data = $request->post();
            $data = $request_data['CardPageForm'];
            $cardpageData = array();
            $cardpageData['banner']     = $data['banner'];
            $cardpageData['page_title'] = $data['page_title'];
            $cardpageData['can_share']  = $data['can_share'] == 1 ? true : false;
            $cardpageData['scene']      = $data['scene'];
            $cardIdsArr                 = $data['card_id'];
            $thumb_urlsArr              = $data['thumb_url'];
            $card_listArr = array();
            foreach($cardIdsArr as $k=>$card_id){
                $card_listArr[$k] = array(
                    'card_id'   => $card_id,
                    'thumb_url' => $thumb_urlsArr[$k]
                );
            }
            $cardpageData['card_list'] = $card_listArr;
            //调用微信接口创建卡券货架
            $cardpage_result = $this->createCardPage($cardpageData);
            //创建失败
            if($cardpage_result['code'] == 0){
                //创建成功，把数据保存在数据库中
                $insertData = array(
                    'cardpage_name' => $data['cardpage_name'],
                    'page_url'      => $cardpage_result['url'],
                    'page_id'       => $cardpage_result['page_id'],
                );
                Yii::$app->db->createCommand()->insert('wx_cardpage', $insertData)->execute();
                Yii::$app->getSession()->setFlash('success', '创建成功');
                return $this->redirect(['index']);
            }else{
                Yii::$app->getSession()->setFlash('error', '创建失败');
                return $this->redirect(['create']);
            }
        }
        $scenelist = $this->getscenelist();
        return $this->render('create', [
            'model' => $cardpageModel,
            'scenelist' => $scenelist,
        ]);
    }

    //获取页面投放场景直
    public function getscenelist(){
        return array(
            //'SCENE_H5 '      => 'h5页面',
            'SCENE_MENU'     => '自定义菜单',
            'SCENE_ARTICLE ' => '公众号文章',
            'SCENE_NEAR_BY ' => '附近',
            'SCENE_QRCODE'   => '二维码',
            'SCENE_IVR'      => '自动回复',
            'SCENE_CARD_CUSTOM_CELL ' => '卡券自定义cell',
        );
    }

    //创建卡券货架
    public function createCardPage($data){
        if(empty($data)){
            return false;
        }
        $cardpage_url = 'https://api.weixin.qq.com/card/landingpage/create?access_token=';  //创建货架接口地址
        $access_token = $this->get_access_token();
        $landingpage_Arr = $this->curlpostJson($cardpage_url.$access_token, $data, true); //post请求
        if($landingpage_Arr['errcode'] != 0){
            $result = array(
                'code' => $landingpage_Arr['errcode'],
            );
        }else{
            $result = array(
                'code' => 0,
                'url'  => $landingpage_Arr['url'],        //货架地址
                'page_id' => $landingpage_Arr['page_id']  //货架ID
            );
        }
        return $result;
    }

    //查看卡券货架
    public function actionView($id){
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    //获取
    public function findModel($id){
        if(($model = CardPageForm::findOne($id)) !== null){
            return $model;
        }
        throw new NotFoundHttpException('The request page does not find');
    }

    //删除卡券货架（只是数据库删除，微信并不会删除）
    public function actionDelete($id)
    {
        $card_info = CouponForm::findOne($id)->toArray();
        if(empty($card_info)){
            return $this->redirect(['index']);
        }
        $this->findModel($id)->delete();
        Yii::$app->getSession()->setFlash('success', '删除成功');
        return $this->redirect(['index']);
    }
}

